package com.patrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.patrol.config.MyPasswordEncoder;
import com.patrol.dao.UserDao;
import com.patrol.pojo.User;
import com.patrol.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Resource
    private MyPasswordEncoder myPasswordEncoder;

    @Override
    public boolean deleteUserById(Integer id) {
        int i = userDao.deleteById(id);
        return i == 1;
    }

    @Override
    public User getUserInformation(String username){
        //使用findByUsername（）方法，判断userCheck是否为空（在数据库中是否存在）
        User userCheck = userDao.findByUsername(username);
        //返回对象
        return userCheck;
    }
    @Override
    public boolean setUserInformation(User user){
//        Integer check = adminUserDao.updateUser(adminUser, username);
        int i = userDao.updateById(user);
        return i == 1;
    }

    @Override
    public String getImgUrl(String username) {
        return userDao.selectUserImgUrl(username);
    }

    @Override
    public boolean registerAdminUser(String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(myPasswordEncoder.md5(password));
        user.setAuthority(0);
        user.setIsAssigned(0);
        user.setIsDeleted(0);
        //使用findByUsername（）方法，判断userCheck是否为空（在数据库中是否存在）
        User userCheck = userDao.findByUsername(user.getUsername());
        if (userCheck == null) {
            user.setAuthority(0);
            //不存在，执行插入操作，注册成功，返回首页面
            return 1 == userDao.insert(user);
        } else {
            return false;
        }
    }

    @Override
    public boolean loginAdminUser(String username, String password) {
        //把前端的username和password写入user对象，方便后续操作
        User user = new User();
        user.setUsername(username);
        user.setPassword(myPasswordEncoder.md5(password));
        //使用findByUsername_login（）方法，对数据库进行检查，判断账号密码是否正确
        User check_login = userDao.findByUsername_login(username, myPasswordEncoder.md5(password));
        return check_login != null;
    }

    @Override
    public User selectUserByUserName(String loginUser) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username", loginUser);
        return userDao.selectOne(qw);
    }

    @Override
    public boolean updatePwdByEmail(String email,String pwd) {
        return userDao.updatePwd(myPasswordEncoder.md5(pwd), email);
    }

    @Override
    public boolean selectUsernameIsExist(String username) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.select("username", username);
        return null == userDao.findByUsername(username);
    }

    /**
     * 头像上传
     *
     * @param
     * @return
     */
    @Override
    public boolean headerImageUpload(User user, String staticPath) {
//        log.info("数据库更新");
        String url = user.getImg();
        //使用findByUrl（）方法，判断userCheck是否为空（在数据库中是否存在）
        User userCheck = userDao.findByUrl(url);
        if (userCheck == null) {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("username", user.getUsername());
            User user1 = userDao.selectOne(qw);
            if (null != user.getImg()) {
                String oldImg = user.getImg();
                File file = new File(staticPath + "/" + oldImg);
                file.delete();
            }
            //不存在，执行插入url操作
            boolean b = userDao.updatePhotoUrl(user.getUsername(), user.getImg()) != 0;
            return b;
        } else {
            return false;
        }
    }

    @Override
    public User selectUserByrealName(String realName) {
        User user = userDao.findByrealName(realName);
        return user;
    }

    @Override
    public List<User> selectAllXG() {
        List<User> list = userDao.selectAllXG();
        return list;
    }

    @Override
    public void changeUserState(String userId) {
        userDao.changeUserState(Integer.parseInt(userId));
    }

    @Override
    public void changeUserKXState(String userId) {
        userDao.changeUserKXState(Integer.parseInt(userId));
    }

    @Override
    public boolean xgUpdatePhotoUrl(Integer userId, String path) {
        Integer integer = userDao.xgUpdatePhotoUrl(userId, path);
        return integer==1;
    }

    @Override
    public User selectUserById(String userId) {
        return userDao.findUserById(Integer.parseInt(userId));
    }

    @Override
    public String selectUserRealNameById(Integer adminId) {
        String realName = userDao.selectUserRealName(adminId);
        return realName;
    }

    @Override
    public int selectXGUserNum() {
        return userDao.selectXGUserNum();
    }

    @Override
    public int selectXGUserStatus(Integer userId) {
        return userDao.selectXGUserStatus(userId);
    }
}
